
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sherin
 */
public class Connection {
    private String serverIP = null;
    private String serverDetails = null;
    private String serverPort = null;
    private StreamConnection connection;
    private OutputStream out = null;
    private InputStream in = null;
    String socket = "8002";
    Connection(String ip, String port) {
        serverIP = ip;
        serverPort = port;
        serverDetails = "socket://" + ip + ":" + port;
    }

     public void connectServer() {
        try {
            System.out.println("trying to connect to the server");
            System.out.println( serverDetails);
            connection = (StreamConnection)Connector.open(serverDetails);
            System.out.println("Connected to " + serverIP + ":" + serverPort );            

        } catch (IOException ex) {
        }
    }

    String VBModify(String string) {
        String message = "VM\nEditVM\n"+string+"\n";
        return message;
    }

    String VBRemove(String string) {
        String message = "VM\nRemoveVM"+string+"\n";
        return message;
    }


    String VBStart(String string) {
        String message = "VM\nStartVM\n"+string+ "\n";
        return message;
    }

    String VBServiceRequest(String action) {
        String message = "VM\n" + action + "\n";
        return message;
    }

    String VBStop(String string) {
        String message = "VM\nStopVM"+string+"\n";
        return message;
    }

    boolean authenticateUser() {
        //check the user
        //return the check to the Client
        return true;
    }

    String authenticateUser(String username, String password) {
        String message = "authentication\n"+ username + "\n" + password;
        return message;
    }

    void writeMessage(String message) throws IOException {
        System.out.println( "here in the writeMessage function ");
         out = connection.openOutputStream();
         out.write(message.getBytes());
         out.flush();
         out.close();
    }
      public String readMessageFromServer() throws IOException {
        String message = null;
        in = connection.openInputStream();
        StringBuffer request = new StringBuffer();
        int cmd = 0;
        System.out.println("before reading the message from the server");
        while(cmd!=-1){
            cmd = in.read();
            if(cmd ==-1 )
                break;
            request.append((char)cmd);
        }
        message = request.toString();

        return( message );
    }
}
